#include <stdlib.h>
#include <jni.h>
#include <android/log.h>

#include "pvm/pvmapi.h"
#include "pvmandroidtestex.h"

JNIEXPORT jint JNICALL 
Java_pvm_test_PvmTestActivity_testPvmInit(JNIEnv* env, jobject thiz, jint nDevice, jint sampleRate, jint bitsPerSample, jint chanelsNum)
{
	PVMDEVICE device = 0;
	int nDriver = 0;
	int devNum = 0;
	__android_log_print(ANDROID_LOG_DEBUG, "pvmandroidtest.c", "%s:%d\n", __FILE__, __LINE__);
	if(!PVMInit())
		return 1;
	__android_log_print(ANDROID_LOG_DEBUG, "pvmandroidtest.c", "%s:%d\n", __FILE__, __LINE__);
	if(!PVMLoadDriver(nDriver))
		return 2;
	__android_log_print(ANDROID_LOG_DEBUG, "pvmandroidtest.c", "%s:%d\n", __FILE__, __LINE__);
	devNum = PVMDevicesOutNum(nDriver);
	if(devNum<=0)
		return 3;
	__android_log_print(ANDROID_LOG_DEBUG, "pvmandroidtest.c", "%s:%d\n", __FILE__, __LINE__);
	device = PVMDeviceOutOpen(nDriver, (int)nDevice, 32, sampleRate, bitsPerSample, chanelsNum, NULL, 0);
	if(device == NULL)
	{
		return 4;
	}

	__android_log_print(ANDROID_LOG_DEBUG, "pvmandroidtest.c", "%s:%d\n", __FILE__, __LINE__);
	jint retVal = (jint)device;
	return retVal;
	//return 35;
}

JNIEXPORT void JNICALL 
Java_pvm_test_PvmTestActivity_testPvmDeinit(JNIEnv* env, jobject thiz, jint device)
{
	PVMDeviceClose((PVMDEVICE)device);

	if(PVMInitialized())
		PVMDeinit();
}

JNIEXPORT jint JNICALL 
Java_pvm_test_PvmTestActivity_testPvmLoadAudioFromArray(JNIEnv* env, jobject thiz, jbyteArray inArray)
{
	PVMAUDIO audio = 0;
	__android_log_write(ANDROID_LOG_DEBUG, "pvmandroidtest.c", "testPvmLoadAudioFromArray -->\n");
	int len = (*env)->GetArrayLength(env, inArray);
	__android_log_write(ANDROID_LOG_DEBUG, "pvmandroidtest.c", "GetArrayLength\n");
	char* data = malloc(len);
	__android_log_write(ANDROID_LOG_DEBUG, "pvmandroidtest.c", "malloc\n");
	(*env)->GetByteArrayRegion(env, inArray, (jint)0, (jint)len, (jbyte*)data);
	__android_log_write(ANDROID_LOG_DEBUG, "pvmandroidtest.c", "GetByteArrayRegion\n");
	audio = loadAudioFromBuffer(data, len);
	__android_log_write(ANDROID_LOG_INFO, "pvmandroidtest.c", "loadAudioFromBuffer\n");
	free(data);
	__android_log_write(ANDROID_LOG_DEBUG, "pvmandroidtest.c", "testPvmLoadAudioFromArray <--\n");
	return (jint)audio;

	return 0;
}

JNIEXPORT jint JNICALL 
Java_pvm_test_PvmTestActivity_testPvmPlayAudio(JNIEnv* env, jobject thiz, jint device, jint audio)
{
	int chanelNum = -1;
	chanelNum = PVMAudioStart((PVMDEVICE)device, (PVMAUDIO)audio, -1, 0, PVM_VOICE_FREE, NULL, 0, NULL);
	return chanelNum;
}

JNIEXPORT void JNICALL 
Java_pvm_test_PvmTestActivity_testPvmFreeAudio(JNIEnv* env, jobject thiz, jint audioHandle)
{
	PVMAudioClose((PVMAUDIO)audioHandle);
}

JNIEXPORT void JNICALL 
Java_pvm_test_PvmTestActivity_testPvmStopAudio(JNIEnv* env, jobject thiz, jint device, jint voiceNum)
{
	PVMVoiceStop((PVMDEVICE)device, (pvm_uint32)voiceNum);
}

JNIEXPORT jint JNICALL 
Java_pvm_test_PvmTestActivity_testPvmSuspendDevice(JNIEnv* env, jobject thiz, jint device)
{
	PVMSuspendDevice((PVMDEVICE)device);
}

JNIEXPORT jint JNICALL 
Java_pvm_test_PvmTestActivity_testPvmResumeDevice(JNIEnv* env, jobject thiz, jint device)
{
	PVMResumeDevice((PVMDEVICE)device);
}
